package edu.jptest.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;

import edu.jptest.bean.TestPoint;
import edu.jptest.service.ITestPointService;
import edu.jptest.serviceImp.TestPointService;

public class TestPointAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8595840994917801235L;
	private TestPoint testPoint;
	private List<TestPoint> testPointList;
	private List<String> typeList;
	private List<String> contentList;
	private Map<String, List<String>> testPointMap;

	public List<String> getTypeList() {
		return typeList;
	}

	public void setTypeList(List<String> typeList) {
		this.typeList = typeList;
	}

	// 根据select的值取contentList
	@SuppressWarnings("unchecked")
	public List<String> getContentList() {
		ValueStack stack = ServletActionContext
				.getValueStack(ServletActionContext.getRequest());
		Object types = stack.findValue("top");
		if (types != null) {
			contentList = (List<String>) testPointMap.get(types);
			return contentList;
		} else {
			return Collections.EMPTY_LIST;
		}
	}

	public void setContentList(List<String> contentList) {
		this.contentList = contentList;
	}

	public Map<String, List<String>> getTestPointMap() {
		return testPointMap;
	}

	public void setTestPointMap(Map<String, List<String>> testPointMap) {
		this.testPointMap = testPointMap;
	}

	public List<TestPoint> getTestPointList() {
		return testPointList;
	}

	public void setTestPointList(List<TestPoint> testPointList) {
		this.testPointList = testPointList;
	}

	public TestPoint getTestPoint() {
		return testPoint;
	}

	public void setTestPoint(TestPoint testPoint) {
		this.testPoint = testPoint;
	}

	// 得到考点列表
	public String getTestPointsList() {
		ITestPointService iTestPointService = new TestPointService();
		testPointList = iTestPointService.getAllTestPoints();
		System.out.println("testpoint: " + testPointList.get(0).getContent());
		return SUCCESS;
	}

	// 添加考点
	public String addTestPoint() {
		ITestPointService iTestPointService = new TestPointService();
		if (iTestPointService.insertTestPoint(testPoint)) {
			return SUCCESS;
		}
		return INPUT;
	}

	// 进入修改考点页面
	public String modifyTestPointInfo() {
		ITestPointService iTestPointService = new TestPointService();
		testPoint = iTestPointService.getTestPointByTestPointId(testPoint
				.getTestPointId());
		return SUCCESS;
	}

	// 修改考点
	public String modifyTestPoint() {
		ITestPointService iTestPointService = new TestPointService();
		if (iTestPointService.updateTestPoint(testPoint)) {
			return SUCCESS;
		}
		return INPUT;
	}

	// 删除考点
	public String deleteTestPoint() {
		ITestPointService iTestPointService = new TestPointService();
		if (iTestPointService.deleteTestPoint(testPoint.getTestPointId())) {
			return SUCCESS;
		}
		return INPUT;
	}

	// 选择题型后得到考点列表
	public String getTestPoints() {
		ITestPointService iTestPointService = new TestPointService();
		testPointList = iTestPointService.getAllTestPoints();
		Iterator<TestPoint> it = testPointList.iterator();

		typeList = new ArrayList<String>();
		testPointMap = new HashMap<String, List<String>>();

		while (it.hasNext()) {
			TestPoint testPoint = (TestPoint) it.next();
			if (!typeList.contains(testPoint.getType())) {
				typeList.add(testPoint.getType());
				List<String> contents = new ArrayList<String>();
				contents.add(testPoint.getContent());
				testPointMap.put(testPoint.getType(), contents);
			} else {
				List<String> contents = testPointMap.get(testPoint.getType());
				if (contents == null) {
					List<String> contents2 = new ArrayList<String>();
					contents2.add(testPoint.getContent());
					testPointMap.put(testPoint.getType(), contents2);
				} else {
					contents.add(testPoint.getContent());
					testPointMap.put(testPoint.getType(), contents);
				}
			}
		}

		return SUCCESS;
	}
}
